$.ajax({
    type: "POST",
    url: constUrl + "ws/task/list",
    dataType: "json",
    data: {operateType: "showTaskDay",},
    success: function (data) {/*获取一个星期的数据 第一个图的数据填充*/
        let history_tasks_week = data.result.history_tasks_week;
        let week_finished = [];
        let week_error = [];
        let week_runing = [];
        let week_pause = [];
        for (let i = 0; i < 7; i++){
            let temp = history_tasks_week[i];
            week_finished.push(parseInt(temp['finished']));
            week_error.push(parseInt(temp['stopped']));
            week_pause.push(parseInt(temp['pause']));
            week_runing.push(parseInt(temp['running']));
        }
        let history_tasks_day = data.result.history_tasks_day;
        let day_finished = [];
        let day_error = [];
        let day_runing = [];
        let day_pause = [];
        for (let i = 0; i < 12; i++){
            let temp = history_tasks_day[i];
            day_finished.push(parseInt(temp['finished']));
            day_error.push(parseInt(temp['stopped']));
            day_runing.push(parseInt(temp['pause']));
            day_pause.push(parseInt(temp['running']));
        }
        let history_tasks_hour = data.result.history_tasks_hour;
        let hour_finished = [];
        let hour_error = [];
        let hour_runing = [];
        let hour_pause = [];
        for (let i = 0; i < 6; i++){
            let temp = history_tasks_hour[i];
            hour_finished.push(parseInt(temp['finished']));
            hour_error.push(parseInt(temp['stopped']));
            hour_runing.push(parseInt(temp['pause']));
            hour_pause.push(parseInt(temp['running']));
        }
        let weeks = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
        let today_week = getWeekends(new Date(), weeks);
        let index = weeks.indexOf(today_week);
        let arr1 = weeks.slice(index + 1);
        let arr2 = weeks.slice(0, index + 1);
        let weeks2 = arr1.concat(arr2);/*填充一天*/
        let times = new Array(13);
        for (let i = 0; i < times.length; i++) {
            let yesterdayTime = new Date();
            yesterdayTime.setHours(yesterdayTime.getHours() - (i * 2));
            times[i] = yesterdayTime;
        }
        times.reverse();
        let dayTime = getTimeArray(times);/*填充一小时*/
        let hourtimes = new Array(8);
        for (let i = 0; i < hourtimes.length; i++) {
            let hourtime = new Date();
            let minutes = 10;
            hourtime.setMinutes(hourtime.getMinutes() - minutes * i);
            hourtimes[i] = hourtime;
        }
        hourtimes.reverse();
        let currentHourTimes = getTimeArray(hourtimes);
        let echartsA = echarts.init(document.getElementById('tpl-echarts-A'));
        let option1 = {
            tooltip: {trigger: 'axis',},
            legend: {data: ['完成', '运行中', '暂停', '失败']},
            grid: {left: '3%', right: '4%', bottom: '3%', containLabel: true},
            xAxis: [{type: 'category', boundaryGap: true, data: weeks2}],
            yAxis: [{type: 'value'}],
            series: [{
                name: '完成',
                type: 'line',
                stack: '总量',
                areaStyle: {normal: {}},
                data: week_finished,
                itemStyle: {normal: {color: '#59aea2'}, emphasis: {}}
            }, {
                name: '失败',
                type: 'line',
                stack: '总量',
                areaStyle: {normal: {}},
                data: week_error,
                itemStyle: {normal: {color: '#e7505a'}}
            }, {
                name: '运行中',
                type: 'line',
                stack: '总量',
                areaStyle: {normal: {}},
                data: week_runing,
                itemStyle: {normal: {color: '#32c5d2'}}
            }, {
                name: '暂停',
                type: 'line',
                stack: '总量',
                areaStyle: {normal: {}},
                data: week_pause,
                itemStyle: {normal: {color: '#e2ba95'}}
            }]
        };
        echartsA.setOption(option1);
        let echartsB = echarts.init(document.getElementById('tpl-echarts-B'));
        let option2 = {
            tooltip: {trigger: 'axis',},
            legend: {data: ['完成', '运行中', '暂停', '失败']},
            grid: {left: '3%', right: '4%', bottom: '3%', containLabel: true},
            xAxis: [{type: 'category', boundaryGap: true, data: dayTime}],
            yAxis: [{type: 'value'}],
            series: [{
                name: '完成',
                type: 'line',
                stack: '总量',
                areaStyle: {normal: {}},
                data: day_finished,
                itemStyle: {normal: {color: '#59aea2'}, emphasis: {}}
            }, {
                name: '失败',
                type: 'line',
                stack: '总量',
                areaStyle: {normal: {}},
                data: day_error,
                itemStyle: {normal: {color: '#e7505a'}}
            }, {
                name: '运行中',
                type: 'line',
                stack: '总量',
                areaStyle: {normal: {}},
                data: day_runing,
                itemStyle: {normal: {color: '#32c5d2'}}
            }, {
                name: '暂停',
                type: 'line',
                stack: '总量',
                areaStyle: {normal: {}},
                data: day_pause,
                itemStyle: {normal: {color: '#e2ba95'}}
            }]
        };
        echartsB.setOption(option2);
        let echartsC = echarts.init(document.getElementById('tpl-echarts-C'));
        let option3 = {
            tooltip: {trigger: 'axis',},
            legend: {data: ['完成', '运行中', '暂停', '失败']},
            grid: {left: '3%', right: '4%', bottom: '3%', containLabel: true},
            xAxis: [{type: 'category', boundaryGap: true, data: currentHourTimes}],
            yAxis: [{type: 'value'}],
            series: [{
                name: '完成',
                type: 'line',
                stack: '总量',
                areaStyle: {normal: {}},
                data: hour_finished,
                itemStyle: {normal: {color: '#59aea2'}, emphasis: {}}
            }, {
                name: '失败',
                type: 'line',
                stack: '总量',
                areaStyle: {normal: {}},
                data: hour_error,
                itemStyle: {normal: {color: '#e7505a'}}
            }, {
                name: '运行中',
                type: 'line',
                stack: '总量',
                areaStyle: {normal: {}},
                data: hour_runing,
                itemStyle: {normal: {color: '#32c5d2'}}
            }, {
                name: '暂停',
                type: 'line',
                stack: '总量',
                areaStyle: {normal: {}},
                data: hour_pause,
                itemStyle: {normal: {color: '#e2ba95'}}
            }]
        };
        echartsC.setOption(option3);
        $(window).resize(function () {
            echartsA.resize();
            echartsB.resize();
            echartsC.resize();
        });
        echartsC.setOption(option3);
        $(window).resize(function () {
            echartsA.resize();
            echartsB.resize();
            echartsC.resize();
        });
        $("#tpl-echarts-B").hide();
        $("#tpl-echarts-C").hide();
        $(".actions-btn").on("click", "#week", function () {
            $("#tpl-echarts-B").hide();
            $("#tpl-echarts-C").hide();
            $("#tpl-echarts-A").show();
            echartsA.resize();
            document.getElementById("week").className = "red-on";
            document.getElementById("day").className = "green";
            document.getElementById("hour").className = "blue";
        });
        $(".actions-btn").on("click", "#day", function () {
            $("#tpl-echarts-A").hide();
            $("#tpl-echarts-C").hide();
            $("#tpl-echarts-B").show();
            echartsB.resize();
            document.getElementById("week").className = "red";
            document.getElementById("day").className = "green-on";
            document.getElementById("hour").className = "blue";
        });
        $(".actions-btn").on("click", "#hour", function () {
            $("#tpl-echarts-B").hide();
            $("#tpl-echarts-A").hide();
            $("#tpl-echarts-C").show();
            echartsC.resize();
            document.getElementById("week").className = "red";
            document.getElementById("day").className = "green";
            document.getElementById("hour").className = "blue-on";
        });
    }
});

function getWeekends(date, weeks) {
    let week;
    if (date.getDay() == 0) week = weeks[0];
    if (date.getDay() == 1) week = weeks[1];
    if (date.getDay() == 2) week = weeks[2];
    if (date.getDay() == 3) week = weeks[3];
    if (date.getDay() == 4) week = weeks[4];
    if (date.getDay() == 5) week = weeks[5];
    if (date.getDay() == 6) week = weeks[6];
    return week;
}

function getNowFormatDate(date) {
    let seperator1 = "-";
    let seperator2 = ":";
    let month = date.getMonth() + 1;
    let strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    let currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
    return currentdate;
}

function getNowFormatDateTime(date) {
    let seperator1 = "-";
    let seperator2 = ":";
    let month = date.getMonth() + 1;
    let strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    let currentdatetime = date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + isZero(date.getHours()) + seperator2 + isZero(date.getMinutes());
    return currentdatetime;
}

$.ajax({
    type: "POST",
    url: constUrl + "AlgorithmServlet",
    dataType: "json",
    data: {operateType: "allAlgorithmNum",},
    success: function (data) {
        $("#algorithmNun").html(data.result);
    }
});
$.ajax({
    type: "POST",
    url: constUrl + "ws/model",
    dataType: "json",
    data: {operateType: "allmodelNum",},
    success: function (data) {
        $("#modelNum").html(data.result);
    }
});
$.ajax({
    type: "POST",
    url: constUrl + "ws/task/list",
    dataType: "json",
    data: {operateType: "getAlltaskNum",},
    success: function (data) {
        $("#taskNum").html(data.result);
    }
});

function getTimeArray(times) {
    let dayTime = new Array(times.length - 1);
    for (let i = 0; i < dayTime.length; i++) {
        dayTime[i] = isZero(times[i + 1].getHours()) + ":" + isZero(times[i + 1].getMinutes());
    }
    return dayTime;
}

function getAllTimeArry(times) {
    let dayTime = new Array(times.length - 1);
    for (let i = 0; i < dayTime.length; i++) {
        dayTime[i] = isZero(times[i + 1].getMonth()) + "-" + isZero(times[i + 1].getDate()) + " " + isZero(times[i + 1].getHours()) + ":" + isZero(times[i + 1].getMinutes());
    }
    return dayTime;
}

function isZero(m) {
    if (m >= 0 && m <= 9) {
        return "0" + m;
    } else {
        return m;
    }
}
